const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/**
 * @desc: 格式化数字
 * @return: n > 10 [eg: 12] => 12 | n < 10 [eg: 3] => '03'
 * @param {*} n
 */

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const wxuuid = function () {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid
}
// 富文本工具类
const formatRichText = (html) => {
  let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"');


  //判断img是否存在
  if (newContent.indexOf('img') != -1) {
    var result = newContent.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
      // console.log(capture);
      return '<img src=' + config().api + capture + ' style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"/>';
    });
    return result;
  }


  return newContent;
}
const config = function () {
  return {

    // api: `http://192.168.0.68:8082/meeting`, //本地
    api: `https://hj.lcxwkj.com/meeting`, //在线

    // api: `http://192.168.0.123:8082/meeting`, //本地
      // api: `https://hj.lcxwkj.com/meeting`, //在线
   //  474846f43bc2d94943d20fab3d70cf9eefe8a89f
    // api: `http://106.13.123.192:8081`, //在线
    // api: 'http://127.0.0.1:8086',
    // 鉴于该小程序有分前端用户和后端用户   区分两个token  一般项目只用前端token即可
    // token: wx.getStorageSync('token'),
    // bToken: wx.getStorageSync('bToken'),
    // app_id: api.appInfo.appId,
    // transaction_id: wxuuid(),
    // req_time: getDateYYYYMMDDHHMISS(),
    // sign:'1234567',
    // user_id:'-1',
    // cookie: '_java110_token_=' + wx.getStorageSync('token')

  }
}
function js_date_time(value) {
  console.log(value)
  if (!value) return '';
  var now = new Date(value );
  console.log("time:" + JSON.stringify(now));
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  if (month < 10) {
    month = '0' + month
  }
  var date = now.getDate();
  if (date < 10) {
    date = '0' + date
  }
  return year + "-" + month + "-" + date
}


module.exports = {
  js_date_time: js_date_time,
  formatTime: formatTime,
  config: config,
  formatRichText:formatRichText
}
